package com.sia.springcloud.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sia.springcloud.controller.IPointController;
import com.sia.springcloud.model.Point;
import com.sia.springcloud.protocol.model.BoschPoint;
import com.sia.springcloud.service.IPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 通信协议变量表 前端控制器
 * </p>
 *
 * @author Sirius
 * @since 2021-12-17
 */
@Slf4j
@RestController
public class PointController implements IPointController {


    @Resource
    private IPointService pointService;


    /**
     * 添加点位
     *
     * @param request
     * @return
     */
    public String insertPoint(HttpServletRequest request) {
        Point point = packingPoint(request);
        return pointService.insertPoint(point);
    }


    /**
     * 分页查询点位
     *
     * @param request
     * @return
     */
    public String queryPoint(HttpServletRequest request) {
        String deviceId = request.getParameter("deviceId");
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));

        return pointService.queryPoint(start, limit, deviceId);
    }

    /**
     * 删除点位
     *
     * @param request
     * @return
     */
    public String deletePoint(HttpServletRequest request) {
        String[] pointIdList = request.getParameter("pointId").split(",");

        return pointService.deletePoint(pointIdList);
    }


    /**
     * 修改点位
     *
     * @param request
     * @return
     */
    public String updatePoint(HttpServletRequest request) {

        Point point = packingPoint(request);

        return pointService.updatePoint(point);
    }

    @Override
    public String tableStructure() {
        return pointService.tableStructure();
    }


    /**
     * 将请求参数包装到一个Point对象中并返回
     *
     * @param request
     * @return
     */
    private Point packingPoint(HttpServletRequest request) {
        String id = request.getParameter("id");
        String collect = request.getParameter("collect");
        String deviceId = request.getParameter("deviceId");
        String protocolName = request.getParameter("protocolName");
        String pointName = request.getParameter("pointName");
        String frequency = request.getParameter("frequency");
        String metricName = request.getParameter("metricName");
        String target = request.getParameter("target");
        String domain = request.getParameter("domain");

        String varKey = request.getParameter("varKey");


        BoschPoint boschPoint = new BoschPoint();
        if (StringUtils.isNotEmpty(id)) {
            boschPoint.setId(Integer.parseInt(id));
        }
        if (StringUtils.isNotEmpty(collect)) {
            boschPoint.setCollect(Integer.parseInt(collect));
        }
        if (StringUtils.isNotEmpty(deviceId)) {
            boschPoint.setDeviceId(Integer.parseInt(deviceId));
        }
        if (StringUtils.isNotEmpty(protocolName)) {
            boschPoint.setProtocolName(protocolName);
        }
        if (StringUtils.isNotEmpty(pointName)) {
            boschPoint.setPointName(pointName);
        }
        if (StringUtils.isNotEmpty(frequency)) {
            boschPoint.setFrequency(Integer.parseInt(frequency));
        }
        if (StringUtils.isNotEmpty(metricName)) {
            boschPoint.setMetricName(metricName);
        }
        if (StringUtils.isNotEmpty(target)) {
            boschPoint.setTarget(target);
        }
        if (StringUtils.isNotEmpty(domain)) {
            boschPoint.setDomain(domain);
        }
        if (StringUtils.isNotEmpty(varKey)) {
            boschPoint.setVarKey(varKey);
        }
        Point point = BoschPoint.convert(boschPoint);

        return point;
    }
}
